package com.example.demo.controller;

import com.example.demo.model.Blog;
import com.example.demo.model.User;
import com.example.demo.service.BlogService;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;

@RestController
public class BlogController {
    @Autowired
    public BlogService blogService;
    @Autowired
    private UserService userService;

    @RequestMapping("/index")
    public List<Blog> getAllBlog() {
        List<Blog> blogs = blogService.getAllBlog();
        for (Blog blog : blogs) {
            if (blog.getContent().length() > 80) {
                blog.setContent(blog.getContent().substring(0,80) + " ...");
            }
        }
        return blogs;
    }

    @RequestMapping("/details")
    public Object getBlogByBid(@SessionAttribute(value = "user",required = false) User user, Integer blogId) {
        HashMap<String,Object> map = new HashMap<>();
        if(blogId == null){
            map.put("message","blogId丢失!");
            return map;
        }
        Blog blog = blogService.getBlogByBid(blogId);
        User author = userService.selectById(blog.getUserId());
        blog.setIsAuthor(author.getUserId() == user.getUserId()? 1 : 0 );
        return blog;
    }

    @RequestMapping("/edit")
    public Object createOneBlog(@SessionAttribute(value = "user" ,required = false) User user,@RequestBody Blog blog) {
        HashMap<String,Object> map = new HashMap<>();
        blog.setContent(blog.getContent().trim());
        blog.setTitle(blog.getTitle().trim());
        if (blog.getContent() == null || "".equals(blog.getContent()) || blog.getTitle() == null || "".equals(blog.getTitle())) {
            map.put("message","正文或者标题不能为空!");
            return map;
        }

        blog.setUserId(user.getUserId());
        blog.setPostTime(new Timestamp(System.currentTimeMillis()));
        blogService.createOneBlog(blog);
        return map;
    }


    @RequestMapping("/logout")
    public void Logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession(false);
        session.removeAttribute("user");
        response.sendRedirect("login.html");
    }


    @RequestMapping("/user")
    public Object getUserName(@SessionAttribute(value = "user",required = false) User user,String blogId){
        if(blogId != null){
            Blog blog= blogService.getBlogByBid(Integer.parseInt(blogId));
            User author = userService.selectById(blog.getUserId());
            return author;
        }else {
            return user;
        }
    }

}
